﻿var FlashInputFieldController = {
	
	FlashID: "gameSwf",
	ContainerID: "FlashInputDiv",
	ImageUrl: "images/FlashTextInput.png",
	
    getFlash: function () {
		var result = document[this.FlashID];
		if (result == null || typeof (result) == "undefined") {
		    result = window[this.FlashID];
		}
		return result;
    },
	
    create: function (name, type) {
		if (document.getElementById(name) != null)  return;
		
		var input = document.createElement(type);
		var div = document.getElementById(this.ContainerID);
		
		input.id = name;
		input.onkeydown = this.onKeyDown;
		input.onkeyup = this.onKeyUp;
		input.onfocus = this.onFocusIn;
		input.onblur = this.onFocusOut;
		input.style.display = "block";
		input.style.position = "absolute";
		input.style.backgroundImage = "url(" + this.ImageUrl + ")";
		input.style.backgroundColor = "transparent";
		input.style.border = "none";
		input.style.outline = "none";
		div.appendChild(input);
    },
    
    remove: function (name) {
		var input = document.getElementById(name);
		var div = document.getElementById(this.ContainerID);
		div.removeChild(input);
    },
    
    show: function (name) {
		var input = document.getElementById(name);
		input.style.display = "block";
    },
	
    hide: function (name) {
		var input = document.getElementById(name);
		input.style.display = "none";
    },
    
    getText: function (name) {
		var input = document.getElementById(name);
		return input.value;
    },
	
    setText: function (name, text) {
		var input = document.getElementById(name);
		input.value = text;
    },
	
    setName: function (name, value) {
		var input = document.getElementById(name);
		if (input == null)  return;
		input.id = value;
    },
	
    update: function (name, text, type, x, y, width, height, font, size, color, maxChars, leading) {
		var input = document.getElementById(name);
		input.maxLength = (maxChars == 0) ? null : maxChars;
		input.style.fontFamily = font;
		input.style.color = color;
		input.style.fontSize = size + "px";
		input.style.left = x + "px";
		input.style.top = y + "px";
		input.style.width = (width - 1) + "px";
		input.style.height = height + "px";
		input.style.overflow = "hidden";
		input.style.leading = leading;
		input.style.resize = "none";
		input.leading = leading;
		input.value = text;
    },
	
    foucsIn: function (name) {
		var input = document.getElementById(name);
		input.focus();
    },
	
    onKeyDown: function (e) {
		var evt = e;
		if (evt == null || typeof (evt) == "undefined") {
		    evt = event;
		}
		
		var target = evt.srcElement ? evt.srcElement : evt.target;

		if(target.value.length > target.maxLength) {
			event.returnValue = false;
		}

		FlashInputFieldController.callback(target.id, evt.keyCode);
		if(evt.keyCode==13) return false;
    },
	
    onKeyUp: function (e) {
		var evt = e;
		if (evt == null || typeof (evt) == "undefined") {
		    evt = event;
		}
		var target = evt.srcElement ? evt.srcElement : evt.target;
		FlashInputFieldController.callback(target.id, evt.keyCode);
		if(evt.keyCode==13) return false;
    },
	
    onFocusIn: function (e) {
		var evt = e;
		if (evt == null || typeof (evt) == "undefined") {
		    evt = event;
		}
		var target = evt.srcElement ? evt.srcElement : evt.target;
		FlashInputFieldController.callback(target.id, "focusIn");
    },
	
    onFocusOut: function (e) {
		var evt = e;
		if (evt == null || typeof (evt) == "undefined") {
		    evt = event;
		}
		var target = evt.srcElement ? evt.srcElement : evt.target;
		FlashInputFieldController.callback(target.id, "focusOut");
    },
	
    callback: function (name, keyCode) {
		var f = this.getFlash();
		if (f == null) return;
		if (f.TextInputCallBack == null) return;
		var input = document.getElementById(name);
		f.TextInputCallBack(name, keyCode, input.value);
    }
	
};